

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>pyamp.patterns.interfaces.requiredProperties &mdash; pyamp v1.2 documentation</title>
    <link rel="stylesheet" href="../../../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../../../',
        VERSION:     '1.2',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../../_static/doctools.js"></script>
    <link rel="top" title="pyamp v1.2 documentation" href="../../../../index.html" />
    <link rel="up" title="pyamp.patterns" href="../../patterns.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li><a href="../../../../index.html">pyamp v1.2 documentation</a> &raquo;</li>
          <li><a href="../../../index.html" >Module code</a> &raquo;</li>
          <li><a href="../../patterns.html" accesskey="U">pyamp.patterns</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <h1>Source code for pyamp.patterns.interfaces.requiredProperties</h1><div class="highlight"><pre>
<span class="c"># Copyright 2012 Brett Ponsler</span>
<span class="c"># This file is part of pyamp.</span>
<span class="c">#</span>
<span class="c"># pyamp is free software: you can redistribute it and/or modify</span>
<span class="c"># it under the terms of the GNU General Public License as published by</span>
<span class="c"># the Free Software Foundation, either version 3 of the License, or</span>
<span class="c"># (at your option) any later version.</span>
<span class="c">#</span>
<span class="c"># pyamp is distributed in the hope that it will be useful,</span>
<span class="c"># but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<span class="c"># MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span>
<span class="c"># GNU General Public License for more details.</span>
<span class="c">#</span>
<span class="c"># You should have received a copy of the GNU General Public License</span>
<span class="c"># along with pyamp.  If not, see &lt;http://www.gnu.org/licenses/&gt;.</span>
<span class="sd">&#39;&#39;&#39;The requiredProperties module contains a class which provides</span>
<span class="sd">an interface which allows a class to define a set of properties</span>
<span class="sd">which the subclasses are required to define. This class handles</span>
<span class="sd">checking that each of those required properties are defined and</span>
<span class="sd">throwing an error in the event that one of them is not defined.</span>

<span class="sd">&#39;&#39;&#39;</span>
<span class="kn">from</span> <span class="nn">pyamp.exceptions</span> <span class="kn">import</span> <span class="n">UndefinedPropertyError</span><span class="p">,</span> <span class="n">InvalidPropertyValueError</span>


<div class="viewcode-block" id="RequiredProperties"><a class="viewcode-back" href="../../../../_generated/pyamp_patterns_interfaces_requiredProperties.html#pyamp.patterns.interfaces.requiredProperties.RequiredProperties">[docs]</a><span class="k">class</span> <span class="nc">RequiredProperties</span><span class="p">:</span>
    <span class="sd">&#39;&#39;&#39;The RequiredProperties class provides an interface which allows</span>
<span class="sd">    subclasses to define a set of properties that must be defined for</span>
<span class="sd">    that class.</span>

<span class="sd">    This class checks that all of those properties are defined, and if</span>
<span class="sd">    one of them is not defined this class throws an exception.</span>

<span class="sd">    &#39;&#39;&#39;</span>
    <span class="n">_RequiredProperties</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="sd">&#39;&#39;&#39;The list of properties that are required to be defined. Each item in</span>
<span class="sd">    the list can be either a single property name which must be defined, or</span>
<span class="sd">    a tuple where the first element in the tuple is the name of the property</span>
<span class="sd">    that must be defined and the second element in the tuple is the value</span>
<span class="sd">    the property must have.</span>

<span class="sd">    .. note:: Subclasses should set this property to the list of</span>
<span class="sd">       properties that are required for the particular class.</span>

<span class="sd">    &#39;&#39;&#39;</span>

    <span class="k">def</span>  <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Check that all the required properties are defined.&#39;&#39;&#39;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__checkRequiredProperties</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>

    <span class="nd">@classmethod</span>
<div class="viewcode-block" id="RequiredProperties.checkRequiredProperties"><a class="viewcode-back" href="../../../../_generated/pyamp_patterns_interfaces_requiredProperties.html#pyamp.patterns.interfaces.requiredProperties.RequiredProperties.checkRequiredProperties">[docs]</a>    <span class="k">def</span> <span class="nf">checkRequiredProperties</span><span class="p">(</span><span class="n">cls</span><span class="p">,</span> <span class="n">obj</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39; &#39;&#39;&#39;</span>
        <span class="n">cls</span><span class="o">.</span><span class="n">__checkRequiredProperties</span><span class="p">(</span><span class="n">obj</span><span class="p">)</span>

    <span class="c">##### Private functions #####</span>
</div>
    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">__checkRequiredProperties</span><span class="p">(</span><span class="n">cls</span><span class="p">,</span> <span class="n">obj</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Check that all of the required properties are defined and</span>
<span class="sd">        throw an error in the event if any are not defined.</span>

<span class="sd">        * obj -- The object to ensure has all its required properties</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="k">for</span> <span class="n">prop</span> <span class="ow">in</span> <span class="n">obj</span><span class="o">.</span><span class="n">_RequiredProperties</span><span class="p">:</span>
            <span class="c"># Determine if the property is only a property name, or if it</span>
            <span class="c"># also includes a required value as well</span>
            <span class="k">if</span> <span class="nb">type</span><span class="p">(</span><span class="n">prop</span><span class="p">)</span> <span class="o">==</span> <span class="nb">type</span><span class="p">(</span><span class="nb">str</span><span class="p">()):</span>
                <span class="n">cls</span><span class="o">.</span><span class="n">__requireProperty</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="n">prop</span><span class="p">)</span>
            <span class="k">elif</span> <span class="nb">type</span><span class="p">(</span><span class="n">prop</span><span class="p">)</span> <span class="o">==</span> <span class="nb">type</span><span class="p">(</span><span class="nb">tuple</span><span class="p">())</span> <span class="ow">and</span> <span class="nb">len</span><span class="p">(</span><span class="n">prop</span><span class="p">)</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
                <span class="c"># Grab the name (first element) and value (second element)</span>
                <span class="n">name</span><span class="p">,</span> <span class="n">value</span> <span class="o">=</span> <span class="n">prop</span>
                
                <span class="n">cls</span><span class="o">.</span><span class="n">__requirePropertyValue</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">raise</span> <span class="n">InvalidPropertyValueError</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="s">&quot;_RequiredProperties&quot;</span><span class="p">,</span>
                                        <span class="n">prop</span><span class="p">,</span> <span class="s">&quot;string, or tuple of length two&quot;</span><span class="p">)</span>

    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">__requireProperty</span><span class="p">(</span><span class="n">cls</span><span class="p">,</span> <span class="n">obj</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Check that the property with the given name is defined.</span>

<span class="sd">        * obj -- The object being checked for the required property</span>
<span class="sd">        * name -- The name of the required property</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
            <span class="k">raise</span> <span class="n">UndefinedPropertyError</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="n">name</span><span class="p">)</span>

    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">__requirePropertyValue</span><span class="p">(</span><span class="n">cls</span><span class="p">,</span> <span class="n">obj</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">expected</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Check that the property with the given name is defined and has</span>
<span class="sd">        the expected value.</span>

<span class="sd">        * obj -- The object being checked for the required property</span>
<span class="sd">        * name -- The name of the required property</span>
<span class="sd">        * expected -- The expected value for the property</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="n">cls</span><span class="o">.</span><span class="n">__requireProperty</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="n">name</span><span class="p">)</span>

        <span class="c"># Ensure that the property has the expected value</span>
        <span class="n">actual</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="n">name</span><span class="p">)</span> 
        <span class="k">if</span> <span class="n">actual</span> <span class="o">!=</span> <span class="n">expected</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">InvalidPropertyValueError</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">actual</span><span class="p">,</span> <span class="n">expected</span><span class="p">)</span></div>
</pre></div>

          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../../../../search.html" method="get">
      <input type="text" name="q" size="18" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li><a href="../../../../index.html">pyamp v1.2 documentation</a> &raquo;</li>
          <li><a href="../../../index.html" >Module code</a> &raquo;</li>
          <li><a href="../../patterns.html" >pyamp.patterns</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
        &copy; Copyright 2012, Brett Ponsler.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.1.
    </div>
  </body>
</html>